package com.demo.study.behavior.d01_chainOfResponsibility.base;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Baymax
 * @since 2021/1/17
 */
public class FilterChain implements Filter {
    List<Filter> filters = new ArrayList<>();

    @Override
    public boolean doFilter(Msg msg) {
        boolean result = false;
        for (Filter filter : filters) {
            boolean success = filter.doFilter(msg);
            if (!success) {
                result = true;
                break;
            }
        }
        if (result) {
            System.out.println("存在非法操作，请检查数据。");
        }
        return true;
    }

    public FilterChain add(Filter filter) {
        filters.add(filter);
        return this;
    }
}
